package com.mudalov.rec.dao.model.study;

import com.mudalov.rec.dao.interfaces.IClonable;
import com.mudalov.rec.dao.model.user.Teacher;
import com.mudalov.rec.util.StringUtil;
import java.io.File;
import java.io.Serializable;

public class Material implements Serializable, IClonable<Material> {
	
	private static final long serialVersionUID = -6675769012954961215L;

	private Long id;
	
	private Subject subject;

	private String name;
	
	private String location;

    private Teacher owner;
	
	public Material() {}
	
	public Material(String name, Teacher owner, Subject subject, String location) {
		this.name = name;
		this.subject = subject;
		this.location = location;
        this.owner = owner;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Subject getSubject() {
		return subject;
	}

	public void setSubject(Subject subject) {
		this.subject = subject;
	}

    public String getSubjectString() {
        if (subject != null) {
            return subject.getText();
        } else {
            return "";
        }
    }

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getLocation() {
		return location;
	}

	public void setLocation(String location) {
		this.location = location;
	}

    public Teacher getOwner() {
        return owner;
    }

    public void setOwner(Teacher owner) {
        this.owner = owner;
    }

    public String getOwnerName() {
        if (this.owner != null) {
            return owner.getFullName();
        } else {
            return "";
        }
    }

    public void setOwnerName() {
        // nothing to do
    }
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result
				+ ((location == null) ? 0 : location.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + ((subject == null) ? 0 : subject.hashCode());
		return result;
	}

    public String getFileName() {
        if (!StringUtil.areEmpty(location)) {
            return location.substring(location.lastIndexOf(File.separator));
        } else {
            return null;
        }
    }

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!(obj instanceof Material))
			return false;
		Material other = (Material) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (location == null) {
			if (other.location != null)
				return false;
		} else if (!location.equals(other.location))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (subject == null) {
			if (other.subject != null)
				return false;
		} else if (!subject.equals(other.subject))
			return false;
		return true;
	}

    public Material getClone() {
        Material clone = new Material();
        clone.setId(this.getId());
        clone.setLocation(this.getLocation());
        clone.setName(this.getName());
        if (this.subject != null) {
            clone.setSubject(this.getSubject().getClone());
        }
        if (this.owner != null) {
            clone.setOwner(this.getOwner().getClone());
        }
        return clone;
    }

}
